package com.laterem.rtc.web;

import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.laterem.rtc.domain.UserDO;
import com.laterem.rtc.service.UserService;
import com.laterem.rtc.util.Result;
import com.laterem.rtc.util.UserHolder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.nio.file.attribute.UserPrincipalNotFoundException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @creator: alan wu
 * @dateTime: 2023/5/27 17:28
 * @desc:
 */
@Tag(name = "拨号软件接口", description = "拨号软件接口")
@RequestMapping("/call")
@RestController
public class CallController {

    @Autowired
    private UserService userService;

    @Operation(summary = "管理用户登录")
    @GetMapping("/admin/login")
    public Result<Object> login(@RequestParam("username")String username,
                                @RequestParam("password")String password) throws UserPrincipalNotFoundException {
        String token = userService.login(username, password, 2);
        if (StringUtils.isBlank(token)){
            throw new UserPrincipalNotFoundException("用户或密码错误");
        }
        Map<String, Object> data = new HashMap<>();
        data.put("token", token);
        data.put("userInfo", UserHolder.getUserDO(token));
        return Result.ok(data);
    }

    @Operation(summary = "普通用户登录")
    @GetMapping("/user/login")
    public Result<Object> userLogin(@RequestParam("username")String username,
                                @RequestParam("password")String password) throws UserPrincipalNotFoundException {
        String token = userService.login(username, password, 1);
        if (StringUtils.isBlank(token)){
            throw new UserPrincipalNotFoundException("用户或密码错误");
        }
        Map<String, Object> data = new HashMap<>();
        data.put("token", token);
        data.put("userInfo", UserHolder.getUserDO(token));
        return Result.ok(data);
    }

    @Operation(summary = "查询手机号码的归属地")
    @GetMapping("/user/location/{cellphone}")
    public Result<Object> location(@PathVariable("cellphone")String cellphone) {
        JSONArray data = userService.location(cellphone);
        return Result.ok(data);
    }

    @Operation(summary = "查询用户列表")
    @GetMapping("/admin/list")
    public Result<Object> userList(Page page)  {
        IPage<UserDO> data = userService.userList(page);
        return Result.ok(data);
    }

    @Operation(summary = "新增用户")
    @PostMapping("/admin/save")
    public Result<Object> save(@RequestBody UserDO userDO)  {
        userDO.setCreateTime(LocalDateTime.now());
        userDO.setUserType(1);
        userService.save(userDO);
        return Result.ok();
    }

    @Operation(summary = "编辑用户")
    @PostMapping("/user/update")
    public Result<Object> update(@RequestBody UserDO userDO)  {
        userService.update(userDO);
        return Result.ok();
    }

    @Operation(summary = "编辑用户")
    @PostMapping("/admin/update")
    public Result<Object> adminUpdate(@RequestBody UserDO userDO)  {
        userService.update(userDO);
        return Result.ok();
    }


    @Operation(summary = "删除用户")
    @GetMapping("/admin/delete/{id}")
    public Result<Object> delete(@PathVariable("id")Integer id)  {
        userService.delete(id);
        return Result.ok();
    }


}
